﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TSP
{
    class Route
    {
        private City[] cities;
        double loopDistance;
        double reverseLoopDistance;

        private void calculateLoopDistance()
        {
            loopDistance = 0.0;
            reverseLoopDistance = 0.0;

            for (int i = 0; i < cities.Length; ++i)
            {
                City start = cities[i];
                City end = cities[(i + 1) % cities.Length];

                loopDistance += start.costToGetTo(end);
                reverseLoopDistance += end.costToGetTo(start);
            }
        }

        public Route(City[] cities)
        {
            this.cities = cities;

            calculateLoopDistance();
        }

        public Route(City[] cities, double loopDistance, double reverseLoopDistance)
        {
            this.cities = cities;
            this.loopDistance = loopDistance;
            this.reverseLoopDistance = reverseLoopDistance;
        }

        public double LoopDistance
        {
            get { return loopDistance; }
        }

        public double ReverseLoopDistance
        {
            get { return reverseLoopDistance; }
        }

        public int Count
        {
            get { return cities.Length; }
        }

        public City this[int index]
        {
            get { return cities[index]; }
        }

        /// <summary>
        /// Returns the shortest route from city to city in order in list form
        /// </summary>
        /// <returns>list of citites in visiting order</returns>
        internal List<City> ToList()
        {
            if (reverseLoopDistance < loopDistance)
                Array.Reverse(this.cities);
            return this.cities.ToList();
        }
    }
}
